package top.anydata.lifeofcode.lifesystem.entity.sys.user;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;

/**
 * 功能描述: 用户映射表
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2023/8/31 5:43 PM
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "sys_auth_user", schema = "life_system_admin", catalog = "")
public class SysAuthUserEntity implements UserDetails {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id")
    private Integer id;
    @Basic
    @Column(name = "username")
    private String username;
    @Basic
    @Column(name = "password")
    private String password;
    @Basic
    @Column(name = "nickname")
    private String nickname;
    @Basic
    @Column(name = "country_code")
    private String countryCode;
    @Basic
    @Column(name = "region_code")
    private String regionCode;
    @Basic
    @Column(name = "gender")
    private Byte gender;
    @Basic
    @Column(name = "birth_date")
    private Date birthDate;
    @Basic
    @Column(name = "email")
    private String email;
    @Basic
    @Column(name = "phone_number")
    private String phoneNumber;
    @Basic
    @Column(name = "avatar_url")
    private String avatarUrl;
    @Basic
    @Column(name = "last_login_time")
    private Timestamp lastLoginTime;
    @Basic
    @Column(name = "registration_time")
    private Timestamp registrationTime;
    @Basic
    @Column(name = "is_deleted")
    private Byte isDeleted;
    @Basic
    @Column(name="is_ban")
    private Byte isBan;
    @Basic
    @Column(name = "create_time")
    private Timestamp createTime;
    @Basic
    @Column(name = "update_time")
    private Timestamp updateTime;
    @Basic
    @Column(name = "remarks")
    private String remarks;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "sys_auth_user_role",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set<SysAuthRoleEntity> roles;


    @Override
    public String getUsername(){
        return this.getEmail();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SysAuthUserEntity that = (SysAuthUserEntity) o;
        return id == that.id && Objects.equals(username, that.username) && Objects.equals(password, that.password) && Objects.equals(nickname, that.nickname) && Objects.equals(countryCode, that.countryCode) && Objects.equals(regionCode, that.regionCode) && Objects.equals(gender, that.gender) && Objects.equals(birthDate, that.birthDate) && Objects.equals(email, that.email) && Objects.equals(phoneNumber, that.phoneNumber) && Objects.equals(avatarUrl, that.avatarUrl) && Objects.equals(lastLoginTime, that.lastLoginTime) && Objects.equals(registrationTime, that.registrationTime) && Objects.equals(isDeleted, that.isDeleted) && Objects.equals(isBan, that.isBan) && Objects.equals(createTime, that.createTime) && Objects.equals(updateTime, that.updateTime) && Objects.equals(remarks, that.remarks);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, username, password, nickname, countryCode, regionCode, gender, birthDate, email, phoneNumber, avatarUrl, lastLoginTime, registrationTime, isDeleted, createTime, updateTime, remarks);
    }

}
